//
//  ViewController.m
//  UI04_TomCatHomework
//
//  Created by dllo on 16/6/11.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "ViewController.h"
#import "SITomCatButton.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatBackground];
    //锣??
    UIImage *firstImage = [UIImage imageNamed:@"cymbal.png"];
    CGRect frame = CGRectMake([UIScreen mainScreen].bounds.size.width * 0.05, [UIScreen mainScreen].bounds.size.height * 0.6, 50, 50);
    NSMutableArray *imageArray = [self creatMutableArrayWithName:@"cymbal_" withCount:12];
    [self creatButtonWithFrame:frame withButtonImage:firstImage withImageArray:imageArray];
    //喝水?牛奶?
    UIImage *secondImage = [UIImage imageNamed:@"drink.png"];
    CGRect frame2 = CGRectMake([UIScreen mainScreen].bounds.size.width * 0.05, [UIScreen mainScreen].bounds.size.height *0.7, 50, 50);
    NSMutableArray *image2Array = [self creatMutableArrayWithName:@"drink_" withCount:80];
    [self creatButtonWithFrame:frame2 withButtonImage:secondImage withImageArray:image2Array];
    //吃东西?
    UIImage *thirdImage = [UIImage imageNamed:@"eat.png"];
    CGRect frame3 = CGRectMake([UIScreen mainScreen].bounds.size.width * 0.05, [UIScreen mainScreen].bounds.size.height *0.8, 50, 50);
    NSMutableArray *image3Array = [self creatMutableArrayWithName:@"eat_" withCount:39];
    [self creatButtonWithFrame:frame3 withButtonImage:thirdImage withImageArray:image3Array];
    //放屁??
    UIImage *fourthImage = [UIImage imageNamed:@"fart.png"];
    CGRect frame4 = CGRectMake([UIScreen mainScreen].bounds.size.width * 0.82, [UIScreen mainScreen].bounds.size.height * 0.6, 50, 50);
    NSMutableArray *image4Array = [self creatMutableArrayWithName:@"fart_" withCount:27];
    [self creatButtonWithFrame:frame4 withButtonImage:fourthImage withImageArray:image4Array];
    //派??
    UIImage *fifthImage = [UIImage imageNamed:@"pie.png"];
    CGRect frame5 = CGRectMake([UIScreen mainScreen].bounds.size.width * 0.82, [UIScreen mainScreen].bounds.size.height * 0.7, 50, 50);
    NSMutableArray *image5Array = [self creatMutableArrayWithName:@"pie_" withCount:23];
    [self creatButtonWithFrame:frame5 withButtonImage:fifthImage withImageArray:image5Array];
    //爪子!!!
    UIImage *sixthImage = [UIImage imageNamed:@"scratch.png"];
    CGRect frame6 = CGRectMake([UIScreen mainScreen].bounds.size.width * 0.82, [UIScreen mainScreen].bounds.size.height * 0.8, 50, 50);
    NSMutableArray *image6Array = [self creatMutableArrayWithName:@"scratch_" withCount:55];
    [self creatButtonWithFrame:frame6 withButtonImage:sixthImage withImageArray:image6Array];
    
    //身体上的按钮
    //头
    CGRect frame7 = CGRectMake([UIScreen mainScreen].bounds.size.width * 0.4, [UIScreen mainScreen].bounds.size.height * 0.2, 100, 100);
    NSMutableArray *image7Array = [self creatMutableArrayWithName:@"knockout_" withCount:80];
    [self creatButtonOnBodyWithFrame:frame7 withImageArray:image7Array];
    //尾巴
    CGRect frame8 = CGRectMake([UIScreen mainScreen].bounds.size.width * 0.65, [UIScreen mainScreen].bounds.size.height * 0.75, 50, 100);
    NSMutableArray *image8Array = [self creatMutableArrayWithName:@"angry_" withCount:25];
    [self creatButtonOnBodyWithFrame:frame8 withImageArray:image8Array];
    //右脚
    CGRect frame9 = CGRectMake([UIScreen mainScreen].bounds.size.width * 0.35, [UIScreen mainScreen].bounds.size.height * 0.9, 60, 70);
    NSMutableArray *image9Array = [self creatMutableArrayWithName:@"footRight_" withCount:29];
    [self creatButtonOnBodyWithFrame:frame9 withImageArray:image9Array];
    //左脚
    CGRect frame10 = CGRectMake([UIScreen mainScreen].bounds.size.width * 0.5, [UIScreen mainScreen].bounds.size.height * 0.9, 60, 70);
    NSMutableArray *image10Array = [self creatMutableArrayWithName:@"footLeft_" withCount:29];
    [self creatButtonOnBodyWithFrame:frame10 withImageArray:image10Array];
    //肚子
    CGRect frame11 = CGRectMake([UIScreen mainScreen].bounds.size.width * 0.4, [UIScreen mainScreen].bounds.size.height * 0.7, 120, 70);
    NSMutableArray *image11Array = [self creatMutableArrayWithName:@"stomach_" withCount:33];
    [self creatButtonOnBodyWithFrame:frame11 withImageArray:image11Array];
}
//创建数组
-(id)creatMutableArrayWithName:(NSString *)name withCount:(int)count {
    NSMutableArray *imageArray = [NSMutableArray arrayWithCapacity:50];
    
    for (int i = 0; i < count + 1; i++) {
        
            NSString *nameForImage = [NSString stringWithFormat:@"%@%02d.jpg",name,i];
            UIImage *image = [UIImage imageNamed:nameForImage];
            [imageArray addObject:image];
        
    }
    return imageArray;

}

//创建背景图
-(void)creatBackground {
    UIImage *backGroundImage = [UIImage imageNamed:@"angry_00.jpg"];
    UIImageView *backGroundView= [[UIImageView alloc] initWithImage:backGroundImage];
    backGroundView.tag = 99;
    backGroundView.frame = [UIScreen mainScreen].bounds;
    [self.view addSubview:backGroundView];
    [backGroundView release];
}
//创建按钮
-(void)creatButtonWithFrame:(CGRect)frame withButtonImage:(UIImage *)image withImageArray:(NSMutableArray *)imageArray{
    SITomCatButton *button = [SITomCatButton buttonWithFrame:frame withUIButtonImage:image withImageArray:imageArray];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(animateButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
//创建身体上的按钮,没什么用,上边的方法少一个参数
-(void)creatButtonOnBodyWithFrame:(CGRect)frame withImageArray:(NSMutableArray *)imageArray{
    SITomCatButton *button = [SITomCatButton buttonWithFrame:frame withUIButtonImage:nil withImageArray:imageArray];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(animateButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}

//调用动画方法
-(void)animateButtonAction:(SITomCatButton *)button{
    UIImageView *imageView = [self.view viewWithTag:99];
    [imageView stopAnimating];
    imageView.animationImages = button.imageArray;
    if (button.imageArray.count > 30){
        imageView.animationDuration = 3;
    } else if (button.imageArray.count > 20){
        imageView.animationDuration = 2;
    } else {
        imageView.animationDuration = 1;
    }
    imageView.animationRepeatCount = 1;
    [imageView startAnimating];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
